package com.islewars.client.lobby;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.islewars.client.ClientStaticData;
import com.islewars.client.battle.TacticalScreen;
import com.islewars.client.dto.ChatLineDto;
import com.islewars.client.dto.GameDto;
import com.islewars.client.service.GameService;
import com.islewars.client.service.GameServiceAsync;

public class LobbyTimer extends Timer {
	private static GameServiceAsync gameServiceAsync = GWT.create(GameService.class);
	private FlexTable playersGrid;
	private FlexTable chatGrid;
	private FlexTable gamesGrid;

	public LobbyTimer(FlexTable playersGrid, FlexTable chatGrid, FlexTable gamesGrid) {
		super();
		this.playersGrid = playersGrid;
		this.chatGrid = chatGrid;
		this.gamesGrid = gamesGrid;
	}

	@Override
	public void run() {
		gameServiceAsync.getPlayers(new AsyncCallback<List<String>>() {
			@Override
			public void onSuccess(List<String> result) {
				playersGrid.clear();
				int row = 0;

				for (String string : result) {
					playersGrid.setWidget(row++, 0, new Label(string));
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				caught.printStackTrace();
			}
		});

		gameServiceAsync.getChat(new AsyncCallback<List<ChatLineDto>>() {
			@Override
			public void onSuccess(List<ChatLineDto> result) {
				chatGrid.clear();
				int row = 0;

				for (ChatLineDto chatLine : result) {
					chatGrid.setWidget(row, 0, new Label(chatLine.player));
					chatGrid.setWidget(row, 1, new Label(chatLine.text));
					row++;
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				caught.printStackTrace();
			}
		});

		gameServiceAsync.getGames(new AsyncCallback<List<GameDto>>() {
			@Override
			public void onSuccess(List<GameDto> result) {
				gamesGrid.clear();
				int row = 0;

				gamesGrid.setWidget(row, 0, new Label("Game name"));
				gamesGrid.setWidget(row, 1, new Label("Creator"));
				row++;

				for (final GameDto game : result) {
					gamesGrid.setWidget(row, 0, new Label(game.name));
					gamesGrid.setWidget(row, 1, new Label(game.creator.name));

					if (!game.inProgress) {
						if (!ClientStaticData.player.equals(game.creator)) {
							Button joinButton = new Button("Join");
							joinButton.addClickHandler(new ClickHandler() {
								@Override
								public void onClick(ClickEvent event) {
									gameServiceAsync.joinGame(ClientStaticData.player, game.name, new AsyncCallback<GameDto>() {
										@Override
										public void onSuccess(GameDto gameDto) {
											new TacticalScreen(gameDto);
										}

										@Override
										public void onFailure(Throwable caught) {
											caught.printStackTrace();
										}
									});
								}
							});
							gamesGrid.setWidget(row, 2, joinButton);
						} else {
							Button deleteButton = new Button("Delete");
							deleteButton.addClickHandler(new ClickHandler() {
								@Override
								public void onClick(ClickEvent event) {
									gameServiceAsync.deleteGame(ClientStaticData.player, game.name, new AsyncCallback<Boolean>() {
										@Override
										public void onSuccess(Boolean result) {
										}

										@Override
										public void onFailure(Throwable caught) {
											caught.printStackTrace();
										}
									});
								}
							});
							gamesGrid.setWidget(row, 3, deleteButton);
						}
					} else {
						gamesGrid.setWidget(row, 2, new Label("In progress"));
					}
					row++;
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				caught.printStackTrace();
			}
		});
	}
}
